package com.example.common;

import com.example.util.R;
import org.springframework.context.annotation.Conditional;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice(annotations = {RestController.class, Conditional.class})
public class GlobalExceptionHandler {

    @ExceptionHandler
    public R<String> doException(SQLIntegrityConstraintViolationException ex)
    {

        ex.printStackTrace();
        if(ex.getMessage().contains("Duplicate entry"))
        {
            //重复添加
            String []  exMessage=ex.getMessage().split(" ");
            return R.error(exMessage[2]+"已经存在");
        }
        return  R.error("服务器异常");
    }


}
